package org.platform.service.system;

import cn.hutool.core.bean.BeanUtil;
import org.apache.commons.lang3.StringUtils;
import org.platform.dao.mapper.FdSysconfigMapper;
import org.platform.mo.csmo.cmd50002.AddSystemParamRequestParam;
import org.platform.mo.csmo.cmd50002.AddSystemParamResponseParam;
import org.platform.mo.csmo.cmd50003.DelSystemParamRequestParam;
import org.platform.mo.csmo.cmd50003.DelSystemParamResponseParam;
import org.platform.mo.csmo.cmd50004.ModifySystemParamRequestParam;
import org.platform.mo.csmo.cmd50004.ModifySystemParamResponseParam;
import org.platform.mo.csmo.cmd50005.QuerySystemParamRequestParam;
import org.platform.mo.csmo.cmd50005.QuerySystemParamResponseParam;
import org.platform.mo.csmo.common.Fd_SysConfig;
import org.platform.mo.db.FdSysconfig;
import org.platform.service.IBaseService;
import org.platform.tool.helper.LogHelper;
import org.platform.tool.util.BeansUtils;
import org.platform.tool.util.ComUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * 系统参数配置 service
 *
 * @author FengJie
 * @date 2018/8/24
 */
@Service
public class SystemParamConfigServiceImpl implements IBaseService {
    @Autowired
    private FdSysconfigMapper sysConfigMapper;
    @Autowired
    private LogHelper logHelper;

    /**
     * 新增系统参数
     */
    public AddSystemParamResponseParam addSystemParam(AddSystemParamRequestParam requestParam) {
        try {
            FdSysconfig fdSysconfig = new FdSysconfig();
            BeanUtil.copyProperties(requestParam.getSysConfig(), fdSysconfig);
            fdSysconfig.setId(ComUtils.getUUID());
            sysConfigMapper.insertSelective(fdSysconfig);
            logHelper.normal("新增系统参数成功", null, null);

        } catch (RuntimeException r) {
            logHelper.error("新增系统参数失败", null, null, r);
        }
        return new AddSystemParamResponseParam();
    }

    /**
     * 删除系统参数配置
     */
    public DelSystemParamResponseParam delSystemParam(DelSystemParamRequestParam requestParam) {
        try {
            sysConfigMapper.deleteByPrimaryKey(requestParam.getId());
            logHelper.normal("删除系统参数成功", null, null);
        } catch (RuntimeException r) {
            logHelper.error("删除系统参数成功", null, null, r);
        }
        return new DelSystemParamResponseParam();
    }

    /**
     * 修改系统参数配置
     */

    public ModifySystemParamResponseParam modifySystemParam(ModifySystemParamRequestParam requestParam) {
        try {
            FdSysconfig fdSysconfig = new FdSysconfig();
            BeanUtil.copyProperties(requestParam.getSysConfig(), fdSysconfig);
            fdSysconfig.setId(ComUtils.getUUID());
            sysConfigMapper.updateByPrimaryKeySelective(fdSysconfig);
            logHelper.normal("修改系统参数成功", null, null);

        } catch (RuntimeException r) {
            logHelper.error("修改系统参数失败", null, null, r);
        }
        return new ModifySystemParamResponseParam();
    }

    /**
     * 查询系统参数配置
     */
    public QuerySystemParamResponseParam querySystemParam(QuerySystemParamRequestParam requestParam) {
        QuerySystemParamResponseParam responseParam = new QuerySystemParamResponseParam();
        try {
            Example example = new Example(FdSysconfig.class);
            Example.Criteria criteria = example.createCriteria();

            if (StringUtils.isNotEmpty(requestParam.getId())) {
                criteria.andEqualTo("id", requestParam.getId());
            }
            List<FdSysconfig> fdSysConfigList = sysConfigMapper.selectByExample(example);
            List<Fd_SysConfig> sysConfigList = BeansUtils.copyListProperties(fdSysConfigList, Fd_SysConfig.class);
            responseParam.setConfigList(sysConfigList);
            logHelper.normal("删除系统参数成功", null, null);
        } catch (RuntimeException r) {
            logHelper.error("删除系统参数成功", null, null, r);
        }
        return responseParam;
    }
}
